# coding:utf-8

import json,sys
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from utils import recipe_utils
from config import recipe_config


def readImg(path, isRemoveWatermark):
    """
    下载图片
    :param path: 图片链接
    :param isRemoveWatermark: 是否去除水印
    :return: 返回oss图片链接
    """
    if recipe_config.UPLOAD_ALI_OSS==False:
        return path
    file_path = recipe_utils.downloadImg(path, recipe_config.FILE_PATH_ROOT)
    if recipe_config.REMOVE_WATER_MARK:
        if isRemoveWatermark:
            file_path = recipe_utils.removeWatermark(file_path)
    path = urlparse(path).path
    return recipe_utils.upload_aliyun_oss(file_path, path[1:])


# 获取菜谱详情
def get_food_detail(food, recipe_ingredient_list, recipe_step_list):
    try:
        soup = BeautifulSoup(recipe_utils.down(food['detail_url']), "lxml")

        # 头部信息
        food['header_img'] = soup.select('.topimgw')[0].select('.headerimg')[0].get("src")
        food["desc"] = soup.select("#cpdes")[0].text

        # 主料
        main_seq = 0
        for main_ingredient_li in soup.select('.c_mtr_ul')[0].select('.c_mtr_li'):
            main_ingredient = {}
            main_seq += 1
            main_ingredient['name'] = main_ingredient_li.select(".t")[0].text
            main_ingredient['measurement'] = main_ingredient_li.select(".a")[0].text
            main_ingredient['type'] = 'main'
            main_ingredient["seq"] = main_seq
            main_ingredient["recipe_code"] = food["recipe_code"]
            recipe_ingredient_list.append(main_ingredient)

        # 辅料
        auxiliary_seq = 0
        for ingredient_li in soup.select('.c_mtr_ul')[1].select('.c_mtr_li'):
            ingredient = {}
            auxiliary_seq += 1
            ingredient['name'] = ingredient_li.select(".t1")[0].text
            ingredient['measurement'] = ingredient_li.select(".a")[0].text
            ingredient['type'] = 'auxiliary'
            ingredient["seq"] = auxiliary_seq
            ingredient["recipe_code"] = food["recipe_code"]
            recipe_ingredient_list.append(ingredient)

        # 步骤
        step_seq = 0
        for step_item in soup.select(".cpc_c4")[0].select(".stepitem"):
            step = {}
            step_seq += 1
            step['step_seq'] = step_seq
            step['step_title'] = step_item.select('.step_title')[0].text
            if step['step_title'] == '成品图':
                step['type'] = 'finished_product_figure'
                step['step_img'] = step_item.select('#imgset')[0].select('img')[0].get("src")
            elif step['step_title'] == '烹饪技巧':
                step['type'] = 'cooking_techniques'
                step['step_des'] = step_item.select('.stepdes')[0].text
            else:
                # 普通步骤
                step['type'] = 'step'
                step_img = step_item.select('.stepimg')
                if step_img:
                    step['step_img'] = step_img[0].get("src")
                step['step_des'] = step_item.select('.stepdes')[0].text
            step["recipe_code"] = food["recipe_code"]
            recipe_step_list.append(step)
    except Exception as e:
        print("获取菜谱详情异常", e)


def get_food_list(url):
    soup = BeautifulSoup(recipe_utils.down(url), "lxml")
    food_list = []
    for li in soup.select(".con_data_s2_list")[0].select("li"):
        a = li.select("a")
        if a:
            food = {}
            food['detail_url'] = a[0].get("href")
            food['logo_img_url_s'] = a[0].select(".cell-img")[0].get("src")
            food['title'] = a[0].select(".title")[0].text
            print(food['detail_url'])
            food_list.append(food)

    return food_list


url = "https://m.meishij.net/fenlei/recai/p"

# 修改页码  也可以加个循环获取
page = 6
recipe_list = []
recipe_ingredient_list = []
recipe_step_list = []
for i in range(page, page + 1):
    food_list = get_food_list(url + str(i))
    for food in food_list:
        food['recipe_code'] = recipe_utils.build_code()
        food['category'] = 'home_cooking_hot_food'
        get_food_detail(food, recipe_ingredient_list, recipe_step_list)
        recipe_list.append(food)

for recipe in recipe_list:
    try:
        recipe['logo_img_url_s'] = readImg(recipe['logo_img_url_s'], False)
        recipe['header_img'] = readImg(recipe['header_img'], False)
        recipe_utils.write2file("C:\\Users\\User\\Desktop\\recipe_list_" + str(page) + ".json",
                         json.dumps(recipe, ensure_ascii=False))
    except Exception as e:
        print("写入数据错误", e)

for recipe_ingredient in recipe_ingredient_list:
    try:

        recipe_utils.write2file("C:\\Users\\User\\Desktop\\recipe_ingredient_list_" + str(page) + ".json",
                         json.dumps(recipe_ingredient, ensure_ascii=False))
    except Exception as e:
        print("写入数据错误", e)

for recipe_step in recipe_step_list:
    try:
        step_img = recipe_step.get('step_img')
        if step_img:
            recipe_step['step_img'] = readImg(step_img, True)
    except Exception as e:
        print("步骤读取图片错误" + recipe_step['step_img'] + "", e)
    print(json.dumps(recipe_step, ensure_ascii=False))
    try:
        recipe_utils.write2file("C:\\Users\\User\\Desktop\\recipe_step_list_" + str(page) + ".json",
                         json.dumps(recipe_step, ensure_ascii=False))
    except Exception as e:
        print("写入数据错误", e)
